<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ include file="/views/common/taglibs.jsp" %>
<head>
    <!-- 引用ztree css样式文件 -->
    <link type="text/css" rel="stylesheet" href="${ctx}/css/widget/ztree/zTreeStyle.css"/>
    <!-- 引用ztree js文件 -->
    <script type="text/javascript" src="${ctx}/js/widget/ztree/jquery.ztree.core.js"></script>
    <script type="text/javascript" src="${ctx}/js/widget/ztree/jquery.ztree.excheck.js"></script>
    <script type="text/javascript" src="${ctx}/js/widget/ztree/jquery.ztree.exedit.js"></script>
    <script type="text/javascript" src="${ctx}/js/common/tree/org_tree.js"></script>
    <script type="text/javascript">
        var setting = {
            view: {
                addDiyDom: addDiyDom,
                selectedMulti: false //是否支持单选或多项(false:单选,true:多选)
            },
            async:{
                enable: false,
                type: "get"
            },
            check: {//是否显示复选框
                enable: false
            },
            data: {
                simpleData: {
                    enable: true
                }
            },
            edit: {//是否添加编辑、删除节点按钮
                drag: {//节点移动配置
                    autoExpandTrigger: true,  //拖拽时父节点是否自动展开(true:是,false:否)
                    isMove: true,     //节点是否可以移动(true:是，false：否)
                    prev: dropPrev,   //移动成为同级前一个节点
                    inner: true, //移动成为子节点
                    next: true    //移动成为同级后一个节点
                },
                enable: true,
                showRemoveBtn: false,
                showRenameBtn: false
            },
            callback: {//树形结构拖拽事件
                beforeDrag: beforeDrag,
                beforeDrop: beforeDrop,
                onDrop: onDrop,
                onClick: onClick,
            }
        };

        function addDiyDom(treeId, treeNode) {
            var spanObj = $("#" + treeNode.tId + "_span");
            var nodeName = treeNode.name;
            if(treeNode.color == "red"){
                var treenodeNames = nodeName.split("(");
                spanObj.empty();
                spanObj.append("<span style='margin-right:0px;'>"+treenodeNames[0]+"</span><span style='color:red;margin-right:0px;'>("+treenodeNames[1]+"</span>");
            }
            //以下代码是添加操作按钮
            <c:if test="${editNode}">
                var aObj = $("#" + treeNode.tId + "_a");
                var addStr = "<span id='diyBtn_" +treeNode.id+ "' title='"+treeNode.name+"' onclick=addDeptNode('"+JSON.stringify(treeNode)+"')><img src='${ctx}/image/common/tree/tree_add.png'/></span>";
                var editStr = "<span id='diyBtn_" +treeNode.id+ "' title='"+treeNode.name+"' onclick=editDeptNode('"+JSON.stringify(treeNode)+"');><img src='${ctx}/image/common/tree/tree_edit.png'/></span>";
                var delStr = "<span id='diyBtn_" +treeNode.id+ "' title='"+treeNode.name+"' onclick=delDeptNode('"+JSON.stringify(treeNode)+"');><img src='${ctx}/image/common/tree/tree_del.png'/></span>";
                aObj.after(addStr).after(editStr).after(delStr);
            </c:if>
        }

        function initTree(datas){
            $.fn.zTree.init($("#menuTree"), setting, datas);
            var treeObj = $.fn.zTree.getZTreeObj("menuTree");
            var nodes = treeObj.getNodes();
            for (var i = 0; i < nodes.length; i++) {
                treeObj.expandNode(nodes[i], true, false, true);
            }
        }

        $(function(){
            initTree(${deptDatas});
        });

        function dropPrev(treeId, nodes, targetNode) {
            var drag = targetNode.drag;
            return drag;
        }

        /**
         * 控制节点是否可以移动回调函数(true:可以，false：不可以)
         * @author      xiaogq
         * @param treeId
         * @param treeNodes
         */
        function beforeDrag(treeId, treeNodes) {
            //判断节点是否可以被拖拽
            var editNode = "${editNode}";
            if(editNode == "true"){
                for (var i = 0; i < treeNodes.length; i++) {
                    var drag = treeNodes[i].drag;
                    if (drag === false) {
                        return false;
                    }
                }
                return true;
            }else{
                return false;
            }
        }

        /**
         * 处理节点是否可以移动到目标节点上(true:可以，false：不可以)
         * @author      xiaogq
         * @param treeId
         * @param treeNodes
         * @param targetNode
         * @param moveType
         * @returns {boolean}
         */
        function beforeDrop(treeId, treeNodes, targetNode, moveType) {
            //console.log("treeNodes==",treeNodes,"&targetNode==",targetNode,"&moveType=",moveType);
            //判断节点是否可以被拖拽
            var editNode = "${editNode}";
            if(editNode == "true"){
                return true;
            }else{
                return false;
            }
        }

        function onDrop(event, treeId, treeNodes, targetNode, moveType){
            //console.log("---treeNodes--",treeNodes, "------targetNode------",targetNode);
            var pId = "",id = "", parentTId = "", oriDeptRank="";
            for (var i = 0; i < treeNodes.length; i++) {
                id += ","+treeNodes[i].id;
                pId = treeNodes[i].pId;
                parentTId = treeNodes[i].parentTId;
                oriDeptRank = treeNodes[i].deptRank;
            }
            var treeObj = $.fn.zTree.getZTreeObj("menuTree");
            var parentTreeNode =  treeObj.getNodeByTId(parentTId);
            var pdeptRank = parentTreeNode.deptRank;
            var orderIds="";
            if (parentTreeNode.isParent){
                var childrens = parentTreeNode.children;
                for(var i = 0; i< childrens.length; i++){
                    orderIds += ","+childrens[i].id;
                }
            }
            id = id.substring(1,id.length);
            if(orderIds != ""){
                orderIds = orderIds.substring(1,orderIds.length);
            }
            if(id != ""){
                $.ajax({
                    url : getRootPath()+"/"+Constants.moveDeptNodeUrl+"?ids="+id+"&pId="+pId +"&pdeptRank="+pdeptRank+"&oriDeptRank="+oriDeptRank+"&orderIds="+ orderIds,
                    type :'POST',
                    success:function(data){
                        var flag = data.flag;
                        if(flag){
                            initTree(data.data);
                        }
                    }
                });
            }
        }

        //全局bootstrap表格搜索变量
        var _SEARCHCONTENT = "";
        /**
         * 部门(组织)点击事件
         * @param e
         * @param treeId
         * @param treeNode
         */
        function onClick(e, treeId, treeNode) {
            //得到组织版本号
            var condition = {deptScope : treeNode.deptRank};
            _SEARCHCONTENT = treeNode.deptRank;
            $("#bootstrapTable").bootstrapTable("refresh", {query:condition});
        }

    </script>
</head>
<!-- 搜索条件div部分start -->
<div class="common_tree">
    <ul id="menuTree" class="ztree"></ul>
</div>
